//
//  GYCartView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/10.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYCartView.h"
#import "Head.h"
#import "GYCartViewModel.h"
@interface GYCartView ()<UITextFieldDelegate>
@property(nonatomic,strong)UILabel* titleLab;
@property(nonatomic,strong)UILabel* protypeLab;
@property(nonatomic,strong)UILabel* priceLab;

@property(nonatomic,strong)UITextField* inputTF;

@property(nonatomic,strong)UILabel* countLab;
@property(nonatomic,strong)UILabel* unityLab;

@property(nonatomic,strong)UIImageView* lineIV;
@property(nonatomic,strong)UIButton* collectBtn;
@property(nonatomic,strong)UIButton* cancelbtn;
@end
@implementation GYCartView


#pragma mark setter getter


-(UITextField *)inputTF
{
    if (!_inputTF) {
        _inputTF=[[UITextField alloc] initWithFrame:WFCGRectMake(220, 107.5, 95, 30)];
        _inputTF.delegate=self;
        _inputTF.textColor=Main_Color_Black;
        _inputTF.keyboardType=UIKeyboardTypeNumberPad;
        _inputTF.textAlignment=NSTextAlignmentCenter;
        _inputTF.text=@"1";
        [WFFunctions WFUIaddbordertoView:_inputTF radius:0 width:0.7 color:Main_Color_LightGray];
    }
    return _inputTF;
}

-(UIButton *)cancelbtn
{
    if (!_cancelbtn) {
        _cancelbtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _cancelbtn.frame=CGRectMake(0, 170,self.width, 45);
    }
    return _cancelbtn;
}

-(UIButton *)collectBtn
{
    if (!_collectBtn) {
        _collectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _collectBtn.frame=CGRectMake(0, WFCGFloatY(171),self.width, WFCGFloatY(45));
        [_collectBtn setBackgroundColor:Main_Color_Red forState:UIControlStateNormal];
        [_collectBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_collectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _collectBtn.titleLabel.font=SYSTEMFONT(15);
        [_collectBtn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _collectBtn;
}
-(UILabel *)unityLab
{
    if (!_unityLab) {
        _unityLab=[[UILabel alloc]initWithFrame:WFCGRectMake(220+100, 107.5, 95, 30)];
        _unityLab.font=SYSTEMFONT(15);
        _unityLab.textColor=Main_Color_Black;
        _unityLab.textAlignment=NSTextAlignmentLeft;
    }
    return _unityLab;
}
-(UIImageView *)lineIV
{
    if (!_lineIV) {
        _lineIV=[[UIImageView alloc] init];
        _lineIV.frame=WFCGRectMake( 15,83,360, WFCGFloatY(0.5));
        _lineIV.backgroundColor=RGBACOLOR(241, 241, 241, 1);
    }
    return _lineIV;
}
-(UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab=[[UILabel alloc] init];
        _titleLab.textColor=Main_Color_Black;
        _titleLab.font=SYSTEMFONT(15);
        
    }
    return _titleLab;
}
-(UILabel *)protypeLab
{
    if (!_protypeLab) {
        _protypeLab=[[UILabel alloc] init];
        _protypeLab.textColor=Main_Color_Red;
        _protypeLab.textAlignment=NSTextAlignmentCenter;
        _protypeLab.font=SYSTEMFONT(10);
    }
    return _protypeLab;
}
-(UILabel *)priceLab
{
    if (!_priceLab) {
        _priceLab=[[UILabel alloc] init];
        _priceLab.textColor=RGBACOLOR(245, 166, 35, 1);
        _priceLab.frame=WFCGRectMake(15, 47, 200, 30);
        _priceLab.font=SYSTEMFONT(17);
    }
    return _priceLab;
}



-(UILabel *)countLab
{
    if (!_countLab) {
        _countLab=[[UILabel alloc] init];
        _countLab.textColor=Main_Color_Black;
        _countLab.font=SYSTEMFONT(15);
        _countLab.frame=WFCGRectMake(15, 111, 200, 20);
        _countLab.text=@"采购数量";
    }
    return _countLab;
}
-(void)press
{
    if ([WFFunctions WFStrCheckEmpty:_inputTF.text])
    {
        [FTIndicator showErrorWithMessage:@"请输入采购数量"];
    }else
    {
        _callback(_inputTF.text);
    }
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled=YES;
        [self addSubview:self.titleLab];
        [self addSubview:self.protypeLab];
        [self addSubview:self.lineIV];
        [self addSubview:self.priceLab];
        [self addSubview:self.inputTF];
        [self addSubview:self.unityLab];
        [self addSubview:self.countLab];
        [self addSubview:self.collectBtn];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_inforModel)
    {

        NSString* nameStr=_inforModel.name;
//        if (nameStr.length>8) {
//            nameStr=[nameStr substringToIndex:8];
//        }
        _titleLab.text=nameStr;
        //_protypeLab.text=_inforModel.protypename;
        
        CGSize titleSize = [WFFunctions WFStrGetSize:_titleLab.text width:MAXFLOAT font:[UIFont systemFontOfSize:15]];
        _titleLab.frame=WFCGRectMake(16, 15.5, titleSize.width, titleSize.height);
        
        CGSize proSize = [WFFunctions WFStrGetSize:_protypeLab.text width:MAXFLOAT font:[UIFont systemFontOfSize:10]];
        _protypeLab.frame=WFCGRectMake(16+titleSize.width+3,15.5+3, proSize.width+8, proSize.height+2);
        [WFFunctions WFUIaddbordertoView:_protypeLab radius:4 width:0.8 color:Main_Color_Red];
        _priceLab.frame=WFCGRectMake(16, 47, 200, 17);
        
        NSString* unitStr=@"/件";
        _priceLab.text=[NSString stringWithFormat:@"%@%@",_inforModel.price,unitStr];
        
        _unityLab.text=@"件";
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_priceLab.text attributes:@{NSFontAttributeName: SYSTEMFONT(21),NSForegroundColorAttributeName:Main_Color_Red}];
        [attributedString addAttribute:NSFontAttributeName value:SYSTEMFONT(12) range:NSMakeRange(_priceLab.text.length-unitStr.length, unitStr.length)];
        _priceLab.attributedText=attributedString;

    }
}

//joinShoppingCar
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
